﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Net.Chirash.Twitter.TweetZone.WindowComponents.ViewModels;

namespace Net.Chirash.Twitter.TweetZone.WindowComponents.Containers
{
    /// <summary>
    /// ChartContainer.xaml の相互作用ロジック
    /// </summary>
    public partial class ChartContainer : Container
    {
        #region Constructors

        public ChartContainer()
            : this(new ChartContainerViewModel())
        {
        }

        public ChartContainer(ChartContainerViewModel viewmodel)
            : base(viewmodel)
        {
            InitializeComponent();

            List<string> periodList = new List<string>() { "Year", "Month", "Day", "Hour" };
            foreach (string period in periodList)
            {
                for (int i = ChartContainerViewModel.GetFirstNumber(period); i <= ChartContainerViewModel.GetLastNumber(period); i++)
                {
                    switch (period)
                    {
                        case "Year":
                            YearComboBox.Items.Add(i); break;
                        case "Month":
                            MonthComboBox.Items.Add(i); break;
                        case "Day":
                            DayComboBox.Items.Add(i); break;
                        case "Hour":
                            HourComboBox.Items.Add(i); break;
                    }
                }
            }
        }

        #endregion

        #region Public Methods

        void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.IsLoaded)
            {
                YearComboBox.IsEnabled = true;
                MonthComboBox.IsEnabled = true;
                DayComboBox.IsEnabled = true;
                HourComboBox.IsEnabled = true;

                switch (GroupingComboBox.SelectedItem.ToString())
                {
                    case "Year":
                        YearComboBox.SelectedValue = "All";
                        YearComboBox.IsEnabled = false;
                        break;
                    case "Month":
                        MonthComboBox.SelectedValue = "All";
                        MonthComboBox.IsEnabled = false;
                        break;
                    case "Day":
                        DayComboBox.SelectedValue = "All";
                        DayComboBox.IsEnabled = false;
                        break;
                    case "Hour":
                        HourComboBox.SelectedValue = "All";
                        HourComboBox.IsEnabled = false;
                        break;
                    default:
                        throw new NotImplementedException();
                }

                ViewModel.RefreshAsync(null);
            }
        }

        #endregion

    }

    public class DateTimeElementValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if ((int)value == -1)
                return "All";
            else
                return ((int)value).ToString();

        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value.ToString() == "All")
            {
                return -1;
            }
            else
            {
                return Int32.Parse(value.ToString());
            }
        }
    }
}
